package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyQueryObject qo) {
        PageInfo pageInfo = strategyService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("regions", regionService.listAll());
        return "/strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult updateState(Strategy strategy, MultipartFile file) {
        JsonResult result = new JsonResult();
        if (file != null && file.getSize() > 0){
            String coverUrl = UploadUtil.uploadQiniu(file);
            strategy.setCoverUrl(coverUrl);
        }
        try {
            strategyService.saveOrUpdate(strategy);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
